<script>
//script for datepicker
var months = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
var blocked_dates = new Array();
var original_blocked_dates = "<?php echo $unit->blocked_dates;?>".split(",");
blocked_dates = original_blocked_dates;
function fill_table(month,month_length) {
	var day=1
	// outputs 3 months in a table row
	var per_row = 3
	var month_number = months.indexOf(month)
	
	if(month_number % per_row == 0 && month_number != 0) document.write("</td></tr><tr><td valign=\"top\">")
	else document.write("</td><td valign=\"top\">")
	
	document.write("<TABLE CELLSPACING=0 CELLPADDING=0><TR>")
	document.write("<TD COLSPAN=7 ALIGN=center class=\"MonthHeader\"><B>"+month+"   "+year+"</B><TR class=\"DayTitle\">")
	day_title("Sun")
	day_title("Mon")
	day_title("Tue")
	day_title("Wed")
	day_title("Thu")
	day_title("Fri")
	day_title("Sat")
	document.write("</TR><TR>")

	for (var i=1;i<start_day;i++) {
		document.write("<TD>")
	}
	for (var i=start_day;i<8;i++) {
		var date = day+"/"+month+"/"+year;
		class_name = original_blocked_dates.inArray(date) ? "Blocked" : "UnBlocked";
		document.write("<TD ALIGN=center><a class=\""+ class_name +"\" id=\"date_"+date+"\" href=\"javascript:void(0)\" onclick=\"block('"+date+"')\">"+day+"</a></TD>")
		day++
	}
	document.write("<TR>")
	while (day <= month_length) {
		for (var i=1;i<=7 && day<=month_length;i++) {
			var date = day+"/"+month+"/"+year;
			class_name = original_blocked_dates.inArray(date) ? "Blocked" : "UnBlocked";
			document.write("<TD ALIGN=center><a class=\""+ class_name +"\" id=\"date_"+date+"\" href=\"javascript:void(0)\" onclick=\"block('"+date+"')\">"+day+"</a></TD>")
			day++
		}
		document.write("</TR><TR>")
		start_day=i
	}
	document.write("</TR></TABLE>")
}
function block(date){
	var ele = document.getElementById("date_"+date);
	if(blocked_dates.inArray(date)){
		blocked_dates.splice(blocked_dates.indexOf(date), 1);
		ele.className = "UnBlocked";
	}
	else{
		blocked_dates.push(date);
		ele.className = "Blocked";
	}
}
function day_title(day_name){
	document.write("<TD ALIGN=center WIDTH=35>"+day_name+"</TD>")
}
function get_months(){
	today= new Date("January 1, "+year)
	start_day = today.getDay() + 1 // retrieves the day(sun, mon, etc...) of the first day of that year
	
	fill_table("January",31)
	if (((year % 4)==0) && ((year % 100)!=0) || ((year % 400)==0))
	{ fill_table("February", 29); }
	else { fill_table("February", 28); }
	fill_table("March",31)
	fill_table("April",30)
	fill_table("May",31)
	fill_table("June",30)
	fill_table("July",31)
	fill_table("August",31)
	fill_table("September",30)
	fill_table("October",31)
	fill_table("November",30)
	fill_table("December",31)
	year = year+1;
}
function select_year(option){
	if(option == 1) cur_year += 1;
	else cur_year -= 1;
	if(cur_year > (year-1)) cur_year = year-1;
	else if(cur_year < start_year) cur_year = start_year;
	$("#year_"+(start_year+0)+", #year_"+(start_year+1)+", #year_"+(start_year+2)+", #year_"+(start_year+3)).css("display", "none");
	$("#year_"+cur_year).css("display", "block");
	$("#CurYear").text(cur_year);
}
function block_dates(){
	$('#blocked_dates').val(blocked_dates.join()); // comma is the default separator for join
	document.calender_form.submit();
}
</script>
<div id="calender">
<div class="Notice">To block dates on your calendar, simply click the date you would like to block. To re-open dates, click on the "x".</div>
<form action="" name="calender_form" id="calender_form" method="post">
	<SCRIPT type="text/javascript" language="JavaScript">
	today = new Date();
	var year = today.getYear(); // changes as years are printed
	if (year < 2000) year = year + 1900; // Y2K Fix, Isaac Powell "http://onyx.idbsu.edu/~ipowell"
	var start_year = year; // the first year (constant)
	var cur_year = year; // the current selected year

	document.write("<div id=\"CalenderWrapper\"><div id=\"CalenderNavigation\"><a href=\"javascript:void(0);\" onclick=\"select_year(-1)\"><img src=\"<?php echo COMPONENT_PATH.'images/year_less.jpg'?>\" /></a>")
	document.write("<span id=\"CurYear\">"+ cur_year +"</span>");
	document.write("<a href=\"javascript:void(0);\" onclick=\"select_year(1)\"><img src=\"<?php echo COMPONENT_PATH.'images/year_more.jpg'?>\" /></a></div>")
	for(var i=0; i<4; i++){
		document.write("<div id=\"year_"+(start_year+i)+"\" style=\"display:" + (i==0 ? 'block':'none') + "\"><table><tr>");
		get_months();
		document.write("</td></tr></table></div>");
	}
	document.write("</div>");
	</SCRIPT>
	<input type="hidden" id="blocked_dates" name="blocked_dates">
	<input type="hidden" name="form" value="save_blocked_dates">
</form>
<?php $KToolBar->addLink('<a href="#" onclick="block_dates()"><img border="0" alt="Save" src="'.ADMIN_COMPONENT_PATH.'images/save_f2.png"/><br />Save</a>');?>
<?php $KToolBar->addLink('<a href="?option=com_resman&user=owner&page=home"><img border="0" alt="Cancel" src="'.ADMIN_COMPONENT_PATH.'images/cancel_f2.png"/><br />Cancel</a>');?>
</div>